/***
 * @Description:这是测试程序的main函数工作
 * @Author: Liverpool
 * @Date: 2023-12-10 02:55:48
 * @LastEditTime: 2024-03-30 03:22:36
 * @LastEditors: Liverpool
 */

#include "main.h"

D_Pcap_Data netifdev0_data = {.ip = netdev0_phy_ip, .hwaddr = netdev0_hwaddr};
/***
 * @description: 网卡配置初始化
 * @Author: Liverpool
 * @Date: 2024-01-23 01:14:36
 * @return {*} 返回网卡驱动是否配置成功
 */
net_err_t netdev_init(void)
{
    D_Net_Interface *netif = net_interface_open("netif 0", &netdev_opreation, &netifdev0_data);
    if (!netif)
    {
        debug_error(Debug_NetInterface, "open netif error");
        return NET_ERR_NONE;
    }
    // 设置回环网络接口的IP地址
    D_IP_ADDR ip, mask, gw;
    ipaddr_from_str(&ip, netdev0_ip);
    ipaddr_from_str(&mask, netdev0_mask);
    ipaddr_from_str(&gw, netdev0_gw);
    net_interface_set_addr(netif, &ip, &mask, &gw); //  设置回环接口的IP地址
    net_interface_set_active(netif);                // 激活网络接口

    D_Packet *buf = packet_alloc(32);
    packet_fill(buf, 0x53, 32);

    D_IP_ADDR dest, src;
    ipaddr_from_str(&dest, "192.168.74.255");
    ipaddr_from_str(&src, netdev0_ip);
    // ipv4_out(0, &dest, &src, buf);
    //  net_interface_out(netif, &dest, buf);

    return NET_ERR_OK;
}
net_err_t message_handler_test_func(struct _message_func msg);
/***
 * @description: 是本项目的测试主函数
 * @Author: Liverpool
 * @Date: 2023-12-10 03:05:28
 * @return {*}
 */
int main(void)
{
    debug_error(D_DEBUG_Level_Info, "liverpool");
    net_init(); // 协议栈初始化
    basic_text();
    feature_text();
    netdev_init(); // 网卡配置初始化
    net_start();   // 协议栈开始
    // 对main进行一直循环，防止程序退出
    D_ping ping;
    ping_run(&ping, friend0_ip, 4, 64, 1000);
    int arg = 0x1234;
    net_err_t err = message_handler_func_execute((message_hander_func)message_handler_test_func, &arg);
    char cmd[32], param[32];
    while (1)
    {
        plat_printf(">>");
        scanf("%s%s", cmd, param);
        if (strcmp(cmd, "ping") == 0)
        {
            ping_run(&ping, param, 4, 64, 1000);
        }
    }
    return 0;
}